package com.manlost.biz.tag.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.manlost.biz.tag.TagService;
import com.manlost.biz.tag.impl.dao.interfaces.BabyTagAccountRelationDAO;
import com.manlost.biz.tag.impl.dao.interfaces.TagDAO;
import com.manlost.biz.tag.impl.dao.interfaces.TagGroupDAO;
import com.manlost.biz.tag.model.constants.TagInfoStatus;
import com.manlost.biz.tag.model.dataobject.BabyTagAccoutRelation;
import com.manlost.biz.tag.model.dataobject.TagGroup;
import com.manlost.biz.tag.model.dto.TagInfoDTO;
import com.manlost.biz.tag.query.ListBabyIdsQuery;
import com.manlost.biz.tag.query.ListBabyTagAccountRelationQuery;
import com.manlost.biz.tag.query.ListTagGroupQuery;
import com.manlost.biz.tag.query.ListTagIdsQuery;
import com.manlost.biz.tag.query.ListTagQuery;

/**
 * 类TagServiceImpl.java的实现描述：TODO 类实现描述
 * 
 * @author xiaochuan.luxc 2012-4-15 下午12:43:03
 */
@Service
public class TagServiceImpl implements TagService {

    @Autowired
    private TagDAO                    tagDAO;
    @Autowired
    private TagGroupDAO               tagGroupDAO;
    @Autowired
    private BabyTagAccountRelationDAO babyTagAccountRelationDAO;

    public List<TagInfoDTO> listTagInfos() {
        List<TagInfoDTO> tagInfos = new ArrayList<TagInfoDTO>();
        List<TagGroup> tagGroups = tagGroupDAO.listTagGroup(new ListTagGroupQuery(TagInfoStatus.ENABLE));
        for (TagGroup tagGroup : tagGroups) {
            TagInfoDTO tagInfo = new TagInfoDTO();
            tagInfo.setTagGroup(tagGroup);
            tagInfo.setTags(tagDAO.listTag(new ListTagQuery(tagGroup.getId())));
            tagInfos.add(tagInfo);
        }
        return tagInfos;
    }

    public List<Long> listBabyIdsByComplexQuery(ListBabyTagAccountRelationQuery query) {
        if (!query.validate()) {
            throw new IllegalArgumentException("listBabyIdsByComplexQuery, query validate error!");
        }
        return babyTagAccountRelationDAO.listBabyIdsByComplexQuery(query);
    }

    public List<Long> listBabyIdsByQuery(ListBabyIdsQuery query) {
        if (!query.validate()) {
            throw new IllegalArgumentException("listBabyIdsByTagQuery, query validate error!");
        }
        return babyTagAccountRelationDAO.listBabyIdsByQuery(query);
    }

    public List<Long> listTagIdsByQuery(ListTagIdsQuery query) {
        if (!query.validate()) {
            throw new IllegalArgumentException("listTagIdsByQuery, query validate error!");
        }
        return babyTagAccountRelationDAO.listTagIdsByQuery(query);
    }

    public Boolean removeBabyTag(BabyTagAccoutRelation record) {
        Boolean result = babyTagAccountRelationDAO.delete(record) > 0;
        return result;
    }

    public Boolean addBabyTag(BabyTagAccoutRelation record) {
        if (record == null || record.getId() != null) {
            throw new IllegalArgumentException("addBabyTag, record is null || record id is not null");
        }
        babyTagAccountRelationDAO.insert(record);
        return record.getId() > 0;
    }

}
